package net.wwang.open.service.impl;

import net.wwang.open.dao.MenuDao;
import net.wwang.open.dao.MenuNodeDao;
import net.wwang.open.global.GlobalTipMsg;
import net.wwang.open.global.ResponseResult;
import net.wwang.open.pojo.Menu;
import net.wwang.open.pojo.vo.MenuNodeVO;
import net.wwang.open.service.MenuService;
import net.wwang.open.util.IDGenerateUtil;
import net.wwang.open.util.JsonUtil;
import net.wwang.open.util.TreeUtil;
import net.wwang.open.util.Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author WangWei
 * @date 2020/8/11
 */
@Slf4j
@Service
@Transactional
public class MenuServiceImpl implements MenuService {

    //默认根ID
    private static String ROOT_STATE_MENU_ID = "-1";
    //目录节点
    private static String MENU_TYPE_DEFAULT = "0";
    //文档节点
    private static String MENU_TYPE_DOCUMENT = "1";

    @Autowired
    private MenuDao menuDao;
    @Autowired
    private MenuNodeDao menuNodeDao;
    @Autowired
    private IDGenerateUtil idGenerator;

    @Override
    public ResponseResult addMenu(Menu menu) {
        // 生成ID
        menu.setId(String.valueOf(idGenerator.nextId()));
        String parentId = menu.getParentId();
        // 读取数据库同目录下的全部目录
        List<Menu> menusFromDb = menuDao.findMenusByParentIdAndName(parentId, menu.getName());
            if (menusFromDb.size() > 0) {
                return ResponseResult.FAILED(GlobalTipMsg.SAME_MENU_ALREADY_EXISTS);
            }
            try {
                menuDao.save(menu);
            } catch (Exception e) {
                log.error("目录保存失败，数据回滚！");
                return ResponseResult.FAILED(GlobalTipMsg.MENU_SAVE_FAILED).setData(menu);
            }
            if(parentId.equals(ROOT_STATE_MENU_ID)){
                return ResponseResult.SUCCESS(GlobalTipMsg.ROOT_MENU_SAVE_SUCCESS).setData(menu);
            }
        return ResponseResult.SUCCESS(GlobalTipMsg.MENU_SAVE_SUCCESS).setData(menu);
    }

    @Override
    public ResponseResult getAllMenus(String space,String order,String sort) {
        // 默认根据创建的先后排序
        List<MenuNodeVO> menus = menuNodeDao.selectAllMenus(space,order,sort);
        TreeUtil treeUtil = new TreeUtil();
        List<Object> objects = treeUtil.treeMenu(menus);
        Object menu = JsonUtil.objectToJsonString(objects);
        return ResponseResult.SUCCESS().setData(menu);
    }

    @Override
    public ResponseResult deleteMenu(String menuId) {
        Menu menu = menuDao.findMenuById(menuId);
        if (menu == null) {
            return ResponseResult.FAILED(GlobalTipMsg.MENU_NOT_EXISTS);
        }
        try {
            deleteAllChildMenu(menuId);
        } catch (Exception e) {
            return ResponseResult.FAILED(GlobalTipMsg.MENU_DELETE_FAILED);
        }
        return ResponseResult.SUCCESS(GlobalTipMsg.MENU_DELETE_SUCCESS);
    }

    @Override
    public ResponseResult updateMenu(String menuId, Menu menu) {
        Menu oldMenu = menuDao.findMenuById(menuId);
        if (oldMenu == null) {
            return ResponseResult.FAILED(GlobalTipMsg.MENU_NOT_EXISTS);
        }
        if (Utils.isEmpty(menu.getName())) {
            return ResponseResult.FAILED(GlobalTipMsg.MENUNAME_NOT_EMPTY);
        }
        oldMenu.setName(menu.getName());
        try {
            menuDao.save(oldMenu);
        } catch (Exception e) {
            return ResponseResult.FAILED(GlobalTipMsg.MENU_UPDATE_FAILED);
        }
        return ResponseResult.SUCCESS(GlobalTipMsg.MENU_UPDATE_SUCCESS);
    }

    public void deleteAllChildMenu(String menuId) {
        //删除当前节点
        menuDao.deleteById(menuId);
        //删除孩子节点
        List<Menu> childrenMenus = menuDao.findMenusByParentId(menuId);
        for (Menu childrenMenu : childrenMenus) {
            String childrenId = childrenMenu.getId();
            deleteAllChildMenu(childrenId);
        }
    }

}
